<!DOCTYPE html "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">


<meta charset="utf-8">
<head th:include="include :: header"></head>
<style>
    .newpeople{display: none}
    .shownum{display: block}
</style>
<body class="gray-bg">
<div class="wrapper wrapper-content ">
    <div class="row">
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="ibox-content">
                    <form class="form-horizontal m-t" id="signupForm">
                        <input type="hidden" id="couponCfgId" name="couponCfgId" th:value="${couponCfgInfo.couponCfgId}">

                        <div class="form-group">
                            <label class="col-sm-2 control-label">分类名称：</label>
                            <div class="col-sm-10">
                                <input id="couponName" name="couponName" th:value="${couponCfgInfo.couponName}"
                                       class="form-control" type="text" >
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">抵扣金额：</label>
                            <div class="col-sm-10">
                                <input type="text" id="discountAmt" name="discountAmt" th:value="${couponCfgInfo.discountAmt}"
                                       data-placeholder="抵扣金额" class="form-control" maxlength="8" onkeyup="value=value.replace(/[^\d\.]/g,'')">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">抵用条件：</label>
                            <div class="col-sm-10">
                                <input type="text" id="discountCondAmt" name="discountCondAmt" th:value="${couponCfgInfo.discountCondAmt}"
                                       placeholder="达到多少金额满减" class="form-control" maxlength="8" onkeyup="value=value.replace(/[^\d\.]/g,'')">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">有效期类型：</label>
                            <div class="col-sm-10">
                                <select name="expireType" id="expireType" class="form-control"
                                        th:field="${couponCfgInfo.expireType}" >
                                    <option value="">--有效期时间--</option>
                                    <option value="1">长期有效</option>
                                    <option value="0">1天</option>
                                    <option value="2">1周</option>
                                    <option value="3">15天</option>
                                    <option value="4">1个月</option>
                                    <option value="5">2个月</option>
                                    <option value="6">3个月</option>
                                    <option value="7">6个月</option>
                                    <option value="8">1年</option>
                                </select>
                            </div>
                        </div>

                        <div class="form-group ">
                            <label class="col-sm-2 control-label">优惠卷类型：</label>
                            <div class="col-sm-10">
                                <select name="couponType" id="couponType" onchange="typeid()" class="form-control"
                                        th:field="${couponCfgInfo.couponType}" >
                                    <option value="2">优惠卷</option>
                                    <option value="1">新人专享卷</option>
                                </select>
                            </div>
                        </div>

                        <div th:class="${couponCfgInfo.couponType}==2 ? 'form-group newpeople':'form-group shownum'">
                            <label class="col-sm-2 control-label">优惠卷总数量：</label>
                            <div class="col-sm-10">
                                <input type="text" id="couponNum" name="couponNum"  class="form-control"
                                       maxlength="8"  th:value="${couponCfgInfo.couponNum}">
                            </div>
                        </div>

                        <div th:class="${couponCfgInfo.couponType}==2 ? 'form-group newpeople':'form-group shownum'">
                            <label class="col-sm-2 control-label">优惠卷剩余数量：</label>
                            <div class="col-sm-10">
                                <input type="text" id="couponRemNum" name="couponRemNum"  class="form-control"
                                       maxlength="8"  th:value="${couponCfgInfo.couponRemNum}">
                            </div>
                        </div>

                        <div class="text-right form-group">
                            <button type="submit" class="btn btn-primary">提交</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
<div th:include="include::footer"></div>
<script th:inline="javascript">
    $().ready(function () {
        validateRule();

    });
    $.validator.setDefaults({
        submitHandler: function () {
            save();
        }
    });

    function typeid() {
        var couponType = $("#couponType").children('option:selected').val();
        console.log(couponType);
        if(couponType==2){
            $(".newpeople").css('display','none');
            $(".shownum").css('display','none');
            $("#couponNum").val(0);
            $("#couponRemNum").val(0);
        }else if(couponType==1){
            $(".newpeople").css('display','block');
            $(".shownum").css('display','block');
        }
    }

    function save() {
        var discountAmt=$("#discountAmt").val();
        var discountCondAmt=$("#discountCondAmt").val();
        console.log(discountAmt,discountCondAmt)
        if(discountAmt-discountCondAmt>0){
            layer.msg("抵扣金额不可大于抵用条件！");
            return false;
        }

        var couponType = $("#couponType").children('option:selected').val();
        if(couponType==1){
            var couponNum=$("#couponNum").val();
            var couponRemNum=$("#couponRemNum").val();
            if(couponNum && couponRemNum){
                if(couponRemNum>couponNum){
                    layer.msg("优惠卷剩余数量不可大于总数量！");
                    return false;
                }
            }else{
                layer.msg("优惠卷剩余数量或总数量不可为空！");
                return false;
            }

        }
        $.ajax({
            cache: true,
            type: "POST",
            url: "/app/couponCfg/update",
            data: $('#signupForm').serialize(), // 你的formid
            async: false,
            success: function (data) {
                layer.msg("操作成功");
                parent.reLoad();
                var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
                parent.layer.close(index);
            }
        });
    }

    function validateRule() {
        var icon = "<i class='fa fa-times-circle'></i> ";
        $("#signupForm").validate({
            rules: {
                couponName: {
                    required: true,
                    remote : {
                        url : "/app/couponCfg/couponCfgNameExit", // 后台处理程序
                        type : "post", // 数据发送方式
                        dataType : "json", // 接受数据格式
                        data : { // 要传递的数据
                            couponName : function() {
                                return $.trim($("#couponName").val());
                            },
                            couponCfgId : function() {
                                return $.trim($("#couponCfgId").val());
                            }
                        }
                    }
                },
                discountAmt:{
                    required: true
                },
                discountCondAmt:{
                    required: true
                },
                expireType:{
                    required: true
                }
            },
            messages: {
                couponName: {
                    required: icon + "必填",
                    remote : icon + "优惠卷名称已存在"
                },
                discountAmt: {
                    required: icon + "必填"
                },
                discountCondAmt: {
                    required: icon + "必填"
                },
                expireType: {
                    required: icon + "必填"
                }
            }
        })
    }

</script>
</body>
</html>